package n7.restaurant.java.Swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextArea;

import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import n7.restaurant.java.Entity.Apprendi;
import n7.restaurant.java.Entity.Chef;
import n7.restaurant.java.Entity.Client;
import n7.restaurant.java.Entity.Ingredient;
import n7.restaurant.java.Entity.Initialisation;
import n7.restaurant.java.Entity.Menu;
import n7.restaurant.java.Entity.Plat;
import n7.restaurant.java.Entity.Serveur;
import n7.restaurant.java.Entity.Specialiste;

public class Swing {

	public static JFrame jFrame;
	public static JPanel panelPlanification;
	public static TextArea textAreaPlanification;
	public static TextArea taC;
	public static TextArea taS;
	public static TextArea taG;
	public static TextArea taSP;
	public static TextArea taA;
	public static JPanel panelEM;
	public static JPanel panelClient;
	public static JPanel panelServeur;
	public static JPanel panelApprendi;
	public static JPanel panelGrandChef;
	public static JPanel panelSpecialiste;
	public static int signalTermine = 0;

	public static String[] TAP = new String[6];
	public static String TAEI = "";
	public static String TAB = "BUT=[entree_pret(C0),plat_principal_pret(C0),dessert(C0),entree_pret(C1),plat_principal_pret(C1),dessert(C1),entree_pret(C2),plat_principal_pret(C2),dessert(C2),entree_pret(C3),plat_principal_pret(C3),dessert(C3),entree_pret(C4),plat_principal_pret(C4),dessert(C4),entree_pret(C5),plat_principal_pret(C5),dessert(C5)]";

	public static String TAC = "";
	public static String TAS = "";
	public static String TAG = "";
	public static String TASP = "";
	public static String TAA = "";
	public static String EI_igs = "";
	public static String EI_ss = "";
	public static String EI_ms = "";
	public static String EI_ps = "";
	public static String EI_cs = "";
	public static String EI_gc = "";
	public static String EI_sps = "";
	public static String EI_aps = "";
	public static String UNIVER = "";

	public static void createAndShowGUI() throws IOException {
		// frame principal
		final JFrame frame = new JFrame("IA Projet - Restaurant");
		jFrame = frame;
		panelPlanification = new JPanel();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(1370, 750));
		frame.getContentPane().add(new AfficheurFixe(), BorderLayout.CENTER);
		frame.getContentPane().add(panelPlanification, BorderLayout.SOUTH);

		// panelPlanification -> JLabel
		panelPlanification.setLayout(new BorderLayout());
		panelPlanification
				.add(new JLabel("Planification:"), BorderLayout.NORTH);
		// text area pour affichier l'etat initial & but & planification &
		// l'univers.
		textAreaPlanification = new TextArea();
		textAreaPlanification.setForeground(Color.WHITE);
		textAreaPlanification.setBackground(Color.BLACK);
		textAreaPlanification.setPreferredSize(new Dimension(jFrame.getWidth(),
				95));
		// // panelPlanification -> textArea
		panelPlanification.add(textAreaPlanification, BorderLayout.SOUTH);

		panelEM = new JPanel();
		panelEM.setPreferredSize(new Dimension(450, 610));
		panelEM.setLayout(new GridLayout(5, 1));
		frame.getContentPane().add(panelEM, BorderLayout.EAST);

		// panel du client
		panelClient = new JPanel();
		panelClient.add(new JLabel("Les Etat Mentaux des Clients:"));
		taC = new TextArea("dfsf");
		taC.setBackground(Color.BLACK);
		taC.setForeground(Color.WHITE);
		taC.setPreferredSize(new Dimension(450, 100));
		panelClient.add(taC);
		panelClient.setBackground(Color.CYAN);
		panelEM.add(panelClient);

		// panel du serveur
		panelServeur = new JPanel();
		panelServeur.add(new JLabel("Les Etat Mentaux des Serveurs"));
		taS = new TextArea("");
		taS.setBackground(Color.BLACK);
		taS.setForeground(Color.WHITE);
		taS.setPreferredSize(new Dimension(450, 100));
		panelServeur.add(taS);
		panelServeur.setBackground(Color.GREEN);
		panelEM.add(panelServeur);

		// panel du grand chef
		panelGrandChef = new JPanel();
		panelGrandChef.add(new JLabel("Les Etat Mentaux du Grand Chef"));
		taG = new TextArea("");
		taG.setBackground(Color.BLACK);
		taG.setForeground(Color.WHITE);
		taG.setPreferredSize(new Dimension(450, 100));
		panelGrandChef.add(taG);
		panelGrandChef.setBackground(Color.GREEN);
		panelEM.add(panelGrandChef);

		// panel du specialiste
		panelSpecialiste = new JPanel();
		panelSpecialiste.add(new JLabel("Les Etat Mentaux des specialiste"));
		taSP = new TextArea("");
		taSP.setBackground(Color.BLACK);
		taSP.setForeground(Color.WHITE);
		taSP.setPreferredSize(new Dimension(450, 100));
		panelSpecialiste.add(taSP);
		panelSpecialiste.setBackground(Color.GREEN);
		panelEM.add(panelSpecialiste);

		// panel d'apprenti
		panelApprendi = new JPanel();
		panelApprendi.add(new JLabel("Les Etat Mentaux des apprentis"));
		taA = new TextArea("");
		taA.setBackground(Color.BLACK);
		taA.setForeground(Color.WHITE);
		taA.setPreferredSize(new Dimension(450, 100));
		panelApprendi.add(taA);
		panelApprendi.setBackground(Color.GREEN);
		panelEM.add(panelApprendi);

		Initialisation.init();

		// difinition du log
		File logs_ei_but_planification = new File(
				"./logs_ei_but_planification.txt");
		File logs_etat_menteux_clients = new File(
				"./logs_etat_menteux_client.txt");
		File logs_etat_menteux_serveurs = new File(
				"./logs_etat_menteux_serveurs.txt");
		File logs_etat_menteux_chef = new File("./logs_etat_menteux_chef.txt");
		File logs_etat_menteux_specialistes = new File(
				"./logs_etat_menteux_specialiste.txt");
		File logs_etat_menteux_apprentis = new File(
				"./logs_etat_menteux_apprentis.txt");
		FileWriter fwp = new FileWriter(logs_ei_but_planification);
		FileWriter fwc = new FileWriter(logs_etat_menteux_clients);
		FileWriter fws = new FileWriter(logs_etat_menteux_serveurs);
		FileWriter fwg = new FileWriter(logs_etat_menteux_chef);
		FileWriter fwsp = new FileWriter(logs_etat_menteux_specialistes);
		FileWriter fwa = new FileWriter(logs_etat_menteux_apprentis);
		final BufferedWriter bwp = new BufferedWriter(fwp);
		final BufferedWriter bwc = new BufferedWriter(fwc);
		final BufferedWriter bws = new BufferedWriter(fws);
		final BufferedWriter bwg = new BufferedWriter(fwg);
		final BufferedWriter bwsp = new BufferedWriter(fwsp);
		final BufferedWriter bwa = new BufferedWriter(fwa);

		// generer les etat initial d'ingredients
		for (Iterator<Ingredient> i = Ingredient.ingredients.iterator(); i
				.hasNext();) {
			Ingredient ig = i.next();
			EI_igs += "lieu(" + ig.getNom() + ")=" + ig.getPosition().getLieu()
					+ ", ";
		}
		
		// generer les etat initial des clients
				for (Iterator<Client> i = Client.clients.iterator(); i
						.hasNext();) {
					Client c = i.next();
					EI_cs += "lieu(" + c.getId() + ")=" + c.getPosition().getLieu()
							+ ", ";
					EI_cs += c.toString();
				}

		// generer les etat initial des serveurs
		for (Iterator<Serveur> i = Serveur.serveurs.iterator(); i.hasNext();) {
			Serveur s = i.next();
			EI_ss += "lieu(" + s.getId() + ")=" + s.getPosition().getLieu()
					+ ", " + "role(" + s.getId() + ")="
					+ s.getRole().toString() + ", " + "etat(" + s.getId()
					+ ")=" + s.getEtat().toString() + ", ";
		}

		// generer les etat initial des menus

		for (Iterator<Menu> i = Menu.menus.iterator(); i.hasNext();) {
			Menu m = i.next();
			EI_ms += m.getId() + "=[";
			for (int j = 0; j < m.getPlats().size(); j++) {
				EI_ms += m.getPlats().get(j).getNom() + ",";
			}
			EI_ms = EI_ms.substring(0, EI_ms.length() - 1);
			EI_ms += "], ";
		}

		// generer les etat initial de grand chef
		EI_gc += "lieu(" + Chef.grand_chef.getId() + ")="
				+ Chef.grand_chef.getPosition().getLieu() + ", ";

		// generer les etat initial de specialiste
		for (Iterator<Specialiste> i = Specialiste.specialistes.iterator(); i
				.hasNext();) {
			Specialiste s = i.next();
			EI_sps += "lieu(" + s.getId() + ")="
					+ s.getPosition().getLieu().toString() + ", ";
			EI_sps += "capacite(" + s.getId() + ")="
					+ s.getCapacite().toString() + ", ";
		}

		// generer les etat initial des plats
		for (Iterator<Plat> i = Plat.plats.iterator(); i.hasNext();) {
			Plat p = i.next();
			EI_ps += "ingredient(" + p.getNom() + ")=[";
			for (int j = 0; j < p.getIngredients().size(); j++) {
				EI_ps += p.getIngredients().get(j) + ",";
			}
			EI_ps = EI_ps.substring(0, EI_ps.length() - 1);
			EI_ps += "], ";
		}
		EI_ps = EI_ps.substring(0, EI_ps.length() - 2);

		// les etat initial
		TAEI = "EI=[" + EI_igs + EI_ss + EI_ms + EI_gc + EI_ps + "]";
		textAreaPlanification.append(TAEI);

		// create new Thread for Clients
		for (Iterator<Client> i = Client.clients.iterator(); i.hasNext();) {
			Client c = i.next();
			new Thread(c).start();
		}

		// create new Thread for Serveurs
		for (Iterator<Serveur> i = Serveur.serveurs.iterator(); i.hasNext();) {
			Serveur s = i.next();
			new Thread(s).start();
		}

		// create new Thread for Grand chef
		new Thread(Chef.grand_chef).start();

		// create new threads for specialiste
		for (Iterator<Specialiste> i = Specialiste.specialistes.iterator(); i
				.hasNext();) {
			Specialiste s = i.next();
			new Thread(s).start();
		}

		// create new threads for apprendis
		for (Iterator<Apprendi> i = Apprendi.apprendis.iterator(); i.hasNext();) {
			Apprendi s = i.next();
			new Thread(s).start();
		}

		frame.pack();
		frame.setVisible(true);

		// create a timer task to repaint.
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				frame.repaint();
			}
		}, 350, 350);

		TAP[0] = "P_1=[";
		TAP[1] = "P_2=[";
		TAP[2] = "P_3=[";
		TAP[3] = "P_4=[";
		TAP[4] = "P_5=[";
		TAP[5] = "P_6=[";

		// affichier les contenu aux panels
		// ecrire les contenu dans les fichier log.
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				if (signalTermine != -1) {
					try {
						// affichier ei & but
						taC.setText("");
						taS.setText("");
						taG.setText("");
						taSP.setText("");
						taA.setText("");
						textAreaPlanification.setText("");
						textAreaPlanification.append(TAEI);
						bwp.write(TAEI);
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAB);
						bwp.write(TAB);

						// affichier l'univers
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append("L'UNIVERS=[");
						bwp.write("L'UNIVERS=[");
						textAreaPlanification.append(UNIVER);
						bwp.write(UNIVER);
						textAreaPlanification.append("]");
						bwp.write("]");

						// affichier 6 planification
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[0] + "]");
						bwp.write(TAP[0] + "]");
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[1] + "]");
						bwp.write(TAP[1] + "]");
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[2] + "]");
						bwp.write(TAP[2] + "]");
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[3] + "]");
						bwp.write(TAP[3] + "]");
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[4] + "]");
						bwp.write(TAP[4] + "]");
						textAreaPlanification.append("\n");
						bwp.write("\n");
						textAreaPlanification.append(TAP[5] + "]");
						bwp.write(TAP[5] + "]");

						// affichier les etat mentaux du client
						for (int i = 0; i < Client.clients.size(); i++) {
							TAC += Client.clients.get(i).toString();
						}
						taC.append(TAC);
						bwc.write(TAC);
						TAC = "";

						// affichier les etat mentaux des serveurs
						for (int i = 0; i < Serveur.serveurs.size(); i++) {
							TAS += Serveur.serveurs.get(i).toString();
						}
						taS.append(TAS);
						bws.write(TAS);
						TAS = "";

						// affichier les etat mentaux de grand chef
						TAG = Chef.grand_chef.toString();
						taG.append(TAG);
						bwg.write(TAG);
						TAG = "";

						// affichier les etat mentaux des specialiste
						for (int i = 0; i < Specialiste.specialistes.size(); i++) {
							TASP += Specialiste.specialistes.get(i).toString();
						}
						taSP.append(TASP);
						bwsp.write(TASP);
						TASP = "";

						// affichier les etat mentaux des apprentis
						for (int i = 0; i < Apprendi.apprendis.size(); i++) {
							TAA += Apprendi.apprendis.get(i).toString();
						}
						taA.append(TAA);
						bwa.write(TAA);
						TAA = "";

						bwp.write("\n");
						bwc.write("\n");
						bws.write("\n");
						bwg.write("\n");
						bwsp.write("\n");
						bwa.write("\n");
						bwp.write("************************************************************");
						bwc.write("************************************************************");
						bws.write("************************************************************");
						bwg.write("************************************************************");
						bwsp.write("************************************************************");
						bwa.write("************************************************************");
						bwp.write("\n");
						bwc.write("\n");
						bws.write("\n");
						bwg.write("\n");
						bwsp.write("\n");
						bwa.write("\n");

						bwp.flush();
						bwc.flush();
						bws.flush();
						bwg.flush();
						bwsp.flush();
						bwa.flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}, 1000, 1000);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					createAndShowGUI();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}